﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Najak.RenderControls
{
	public class XnaServices : IServiceProvider
	{
		private GameServiceContainer _xnaServices;

		public XnaServices(GameServiceContainer xnaServices)
		{
			_xnaServices = xnaServices;
		}
		public object GetService(Type serviceType)
		{
			return _xnaServices.GetService(serviceType);
		}
	}

	public class AppHost : Game, IAppHost
	{
		GraphicsDeviceManager _graphics;
		App _app;

		public IServiceProvider Services { get; private set; }

		public AppHost()
		{
			_graphics = new GraphicsDeviceManager(this);
			_graphics.GraphicsProfile = GraphicsProfile.HiDef;
			_graphics.PreferredBackBufferHeight = 1200;
			_graphics.PreferredBackBufferWidth = 1920;
			_graphics.ToggleFullScreen();

			Content.RootDirectory = "Content";
			Services = new XnaServices(base.Services);

			_app = App.Create(this, Content);
			IsMouseVisible = true;
		}

		protected override void LoadContent()
		{
			base.LoadContent();
			_app.LoadContent();
		}
		protected override void UnloadContent()
		{
			base.UnloadContent();
		}
		protected override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			_app.Update(gameTime);

			if (false) //!_graphics.IsFullScreen && gameTime.TotalGameTime.TotalSeconds > 7)
			{
				_isFullScreen = true;
				_graphics.PreferredBackBufferHeight = 1200;
				_graphics.PreferredBackBufferWidth = 1920;
				_graphics.ToggleFullScreen();
			}
		}
		private bool _isFullScreen;

		protected override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);
			_app.Draw();
		}


		public Rectangle ScreenArea
		{
			get { return base.Window.ClientBounds; }
		}

		public Point ScreenLocation
		{
			get 
			{ 
				Rectangle area = base.Window.ClientBounds;
				return new Point(area.X, area.Y);
			}
		}

		public Point ScreenSize
		{
			get 
			{ 
				Rectangle area = base.Window.ClientBounds;
				return new Point(area.Width, area.Height);
			}
		}

		public bool IsMousePositionAlreadyCorrect
		{
			get { return true; }
		}
	}
}
